// onload actions

$(function(){
	$.ajaxSetup({cache:false,type:'POST'});
	$('.hide').hide();
	switchToPanel('thread');
	$(".wait").ajaxStart(function(){$(this).show();});
	$(".wait").ajaxStop(function(){$(this).hide();});
});
function setPermission(level) {
	$('.permissions').hide();
	if(level=='anon') {
		$('.onlyAnon, .belowUser').show();
	} else if(level=='user') {
		$('.aboveUser, .onlyUser, .belowUser').show();
	} else {
		$('.onlyMod, .aboveUser').show();
	}
}

// panel buttons

function switchToPanel(panelName) {
	$('.panelSection:visible').hide();
		$('.panelTitle').html(panelName);
		$('.'+panelName+'Panel').show();
}

function openSignUp() {
	switchToPanel('account');
	$('.accountPanel .subpanel:visible').hide();
	$('.accountPanel .subpanel.signUp').show();
}

function openLogIn() {
	switchToPanel('account');
	$('.accountPanel .subpanel:visible').hide();
	$('.accountPanel .subpanel.notLogged').show();
}

// ajax functions

function logIn() {
	$.post('panels/logIn.php',{u:$('#logUser').val(),p:$('#logPass').val()},function(r) {
		if(r.r=='ok') {
			$('.accountPanel .subpanel').hide();
			setPermission(r.p);
			$('.accountPanel .subpanel.logged .subpanelTitle .userName').html(r.u);
		} else {
			$('.accountPanel .subpanel').hide();
			$('.accountPanel .subpanel.logInfo').show();
			$('.accountPanel .subpanel.logInfo .subpanelTitle').html(r.title);
			$('.accountPanel .subpanel.logInfo .textBlock').html(r.text);
			$('.accountPanel .subpanel.logInfo .hotlink').html('<a href="javascript:openLogIn();">' + r.hotlink + '</a>');
		}
	},'json');
}

function signUp() {
	$.post('panels/signUp.php',{u:$('#signUser').val(),p:$('#signPass').val()},function(r) {
		if(r.r=='ok') {
			$('.accountPanel .subpanel').hide();
			setPermission('user');
			$('.accountPanel .subpanel.logged .subpanelTitle .userName').html($('#signUser').val());
		} else {
			$('.accountPanel .subpanel').hide();
			$('.accountPanel .subpanel.logInfo').show();
			$('.accountPanel .subpanel.logInfo .subpanelTitle').html(r.title);
			$('.accountPanel .subpanel.logInfo .textBlock').html(r.text);
			$('.accountPanel .subpanel.logInfo .hotlink').html('<a href="javascript:openSignUp();">' + r.hotlink + '</a>');
		}
	},'json');
}

function logOff() {
	$.post('panels/logOff.php',null,function(r) {
		if(r.r=='ok') {
			$('.accountPanel .subpanel').hide();
			setPermission('anon');
		}
	},'json');
}